package org.example;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;

/**
 * 计算放款总额是否达到500万
 */
public class TestLoanAmountCalculation {
    
    public static void main(String[] args) {
        System.out.println("=== 放款总额计算验证 ===");
        
        // 配置参数
        LocalDate startDate = LocalDate.of(2023, 7, 1);
        LocalDate currentDate = LocalDate.now();
        int baseOrders = 300;
        int monthlyIncrease = 30;
        
        // 贷款金额配置
        int minLoanAmount = 3000; // 3000元
        int maxLoanAmount = 7000; // 7000元
        int avgLoanAmount = (minLoanAmount + maxLoanAmount) / 2; // 5000元
        
        System.out.println("贷款金额配置:");
        System.out.println("- 最小金额: " + minLoanAmount + " 元");
        System.out.println("- 最大金额: " + maxLoanAmount + " 元");
        System.out.println("- 平均金额: " + avgLoanAmount + " 元");
        
        System.out.println("\n订单数量配置:");
        System.out.println("- 基础订单数: " + baseOrders + " 个/月");
        System.out.println("- 每月递增: " + monthlyIncrease + " 个");
        
        // 计算总订单数
        LocalDate month = startDate;
        int totalOrders = 0;
        int monthCount = 0;
        
        System.out.println("\n=== 每月订单数计算 ===");
        while (month.isBefore(currentDate)) {
            int monthsFromStart = (int) (month.toEpochDay() - startDate.toEpochDay()) / 30;
            int monthlyOrders = baseOrders + (monthsFromStart * monthlyIncrease);
            totalOrders += monthlyOrders;
            monthCount++;
            
            System.out.println("月份: " + month + ", 订单数: " + monthlyOrders);
            
            month = month.plusMonths(1);
        }
        
        // 计算总放款金额
        BigDecimal totalLoanAmount = BigDecimal.valueOf(totalOrders).multiply(BigDecimal.valueOf(avgLoanAmount));
        
        System.out.println("\n=== 计算结果 ===");
        System.out.println("总月数: " + monthCount);
        System.out.println("总订单数: " + totalOrders);
        System.out.println("平均贷款金额: " + avgLoanAmount + " 元");
        System.out.println("总放款金额: " + totalLoanAmount.divide(new BigDecimal("10000"), 2, RoundingMode.HALF_UP) + " 万");
        
        // 验证是否达到目标
        BigDecimal targetLoanAmount = new BigDecimal("5000000"); // 500万
        
        System.out.println("\n=== 目标验证 ===");
        System.out.println("目标放款金额: 500万");
        System.out.println("实际放款金额: " + totalLoanAmount.divide(new BigDecimal("10000"), 2, RoundingMode.HALF_UP) + " 万");
        
        if (totalLoanAmount.compareTo(targetLoanAmount) >= 0) {
            System.out.println("✅ 放款金额达到目标");
        } else {
            System.out.println("❌ 放款金额未达到目标");
            BigDecimal shortage = targetLoanAmount.subtract(totalLoanAmount);
            System.out.println("缺口: " + shortage.divide(new BigDecimal("10000"), 2, RoundingMode.HALF_UP) + " 万");
            
            // 计算需要调整的订单数
            BigDecimal neededAmount = shortage.divide(BigDecimal.valueOf(avgLoanAmount), 0, RoundingMode.HALF_UP);
            System.out.println("需要增加订单数: " + neededAmount + " 个");
        }
        
        // 如果需要调整，计算新的配置
        if (totalLoanAmount.compareTo(targetLoanAmount) < 0) {
            System.out.println("\n=== 调整建议 ===");
            
            // 方案1：增加基础订单数
            BigDecimal neededOrders = targetLoanAmount.divide(BigDecimal.valueOf(avgLoanAmount), 0, RoundingMode.HALF_UP);
            int newBaseOrders = neededOrders.divide(BigDecimal.valueOf(monthCount), 0, RoundingMode.HALF_UP).intValue();
            System.out.println("方案1 - 调整基础订单数: " + baseOrders + " -> " + newBaseOrders);
            
            // 方案2：增加每月递增数
            int newMonthlyIncrease = (neededOrders.intValue() - totalOrders) / monthCount;
            System.out.println("方案2 - 调整每月递增: " + monthlyIncrease + " -> " + (monthlyIncrease + newMonthlyIncrease));
            
            // 方案3：增加贷款金额
            BigDecimal newAvgLoanAmount = targetLoanAmount.divide(BigDecimal.valueOf(totalOrders), 0, RoundingMode.HALF_UP);
            System.out.println("方案3 - 调整平均贷款金额: " + avgLoanAmount + " -> " + newAvgLoanAmount);
        }
    }
}